"""
文件名: UmsMemberReceiveAddressControllerr.py
作用: 用户优惠券管理模块的接口,每一个接口都有一个对应的方法,该方法用于测试用例
作者: 严啸玲
"""
import pytest
from Untils.SendMethod import SendMethod
from Untils.GetKeyword import GetKeyword
from Interface.DHX_MemberInterface import MemberInterface


class UmsMemberReceiveAddressControllerr:
    def __init__(self):
        self.url = 'http://139.159.146.104:8083'

    def member_login(self, username, password):
        """
        会员登录接口
        :param username: 用户名
        :param password: 密码
        :return:
        """
        # 请求地址
        url = self.url + "/sso/login"
        # 请求数据
        payload = {
            'username': username,
            'password': password
        }
        return SendMethod.send_method(method='post', url=url, data=payload)

    def get_token(self, username, password):
        """
        获取会员登录token
        :param username: 用户名
        :param password: 密码
        :return: 拼接后的token请求头信息
        """
        token = GetKeyword.get_keyword(self.member_login(username, password), 'token')
        return {"Authorization": f'Bearer {token}'}

    def get_id(self):
        """
         获取显示全部地址id
        :param username:用户名
        :param password: 密码
        :return:
        """
        id = GetKeyword.get_keyword(self.member_address_list(username='liar', password='123456'), 'id')
        return id

    def get_id_number(self, username, password):
        """
        获取id数字
        :param username: 用户名
        :param password: 密码
        :return:
        """

    def member_address_add(self, username, password):
        """
        添加收获地址
        :param address: 住址
        :param username: 用户名
        :param password: 密码
        :return:
        """
        # 请求地址
        url = self.url + "/member/address/add"
        # 请求数据
        address = {
            "city": "成都市",
            "defaultStatus": 1,
            "detailAddress": "聚霞路",
            "id": 0,
            "memberId": 2,
            "name": "liar",
            "phoneNumber": "18312345678",
            "postCode": "666666",
            "province": "四川省",
            "region": "金牛区"
        }
        headers = self.get_token(username, password)
        return SendMethod.send_method(method='post', url=url, json=address, headers=headers)

    def member_address_list(self, username, password):
        """
        显示所有收货地址
        :param address: 住址
        :param username: 用户名
        :param password: 密码
        :return:
        """
        # 请求地址
        url = self.url + "/member/address/list"
        headers = self.get_token(username, password)
        return SendMethod.send_method(method='get', url=url, headers=headers)

    def member_address_update(self, username, password):
        """
        修改收货地址
        :param address: 住址
        :param username: 用户名
        :param password: 密码
        :return:
        """
        # 请求地址
        url = self.url + f"/member/address/update/{self.get_id()}"
        # 请求数据
        payload = {
            "city": "成都市",
            "defaultStatus": 1,
            "detailAddress": "殷俊",
            "memberId": 2,
            "id": self.get_id(),
            "name": 'liar',
            "phoneNumber": "13540244558",
            "postCode": "666666",
            "province": "四川省",
            "region": "武侯区"
        }
        headers = self.get_token(username, password)
        return SendMethod.send_method(method='post', url=url, json=payload, headers=headers)

    def member_address_id(self, id, username, password):
        """
        显示所有收货地址id
        :param address: 住址
        :param username: 用户名
        :param password: 密码
        :return:
        """
        # 请求地址
        url = self.url + f"/member/address/{id}"
        # 请求数据
        id = {id: '199'}
        headers = self.get_token(username, password)
        return SendMethod.send_method(method='get', url=url, headers=headers, params=id)

    def member_address_delete_id(self, username, password):
        """
        删除收货地址id
        :param address: 住址
        :param username: 用户名
        :param password: 密码
        :return:
        """
        # 请求地址
        url = self.url + f"/member/address/delete/{self.get_id()}"
        # 请求数据
        headers = self.get_token(username, password)
        return SendMethod.send_method(method='post', url=url, headers=headers)


if __name__ == '__main__':
    addressadd = UmsMemberReceiveAddressControllerr()
    # print(addressadd.member_address_add(username='liar', password='123456')) # 添加收获地址
    # print(addressadd.get_id())
    # print(addressadd.member_address_update(username='liar', password='123456', id=83)) # 修改收货地址
    # print(addressadd.member_address_id(id=51, username='liar', password='123456'))  # 显示所有收货地址id
    # print(addressadd.member_address_delete_id(username='liar', password='123456'))
    print(addressadd.member_address_list(username='liar', password='123456'))  # 显示所有收货地址
